package com.gaoyubo.service;

import com.gaoyubo.entity.Insurance;
import com.gaoyubo.entity.Insurance_type;
import com.gaoyubo.entity.SearchVo;
import com.gaoyubo.entity.User;
import com.gaoyubo.mapper.InsuranceMapper;
import com.gaoyubo.mapper.InsuranceTypeMapper;
import com.gaoyubo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
public class InsuranceServiceImpl implements InsuranceService {
    @Autowired
    private InsuranceMapper insuranceMapper;
    @Autowired
    private InsuranceTypeMapper insuranceTypeMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public Page<Insurance> findAll(Integer pageNum, Integer pageSize, SearchVo searchVo) {
        Specification<Insurance> spec= new Specification<Insurance>() {
            @Override
            public Predicate toPredicate(Root<Insurance> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                List<Predicate> list =new ArrayList<>();
                if(searchVo.getUser_name1()!=null){
                    Predicate uname1 = cb.like(root.get("users").get("User_name"), "%" + searchVo.getUser_name1() + "%");
                    list.add(uname1);
                }
                if(searchVo.getUser_name2()!=null){
                    Predicate uname2 = cb.like(root.get("users").get("User_name"), "%" + searchVo.getUser_name2() + "%");
                    list.add(uname2);
                }
                if(searchVo.getUser_name3()!=null){
                    Predicate uname3 = cb.like(root.get("users").get("User_name"), "%" + searchVo.getUser_name3() + "%");
                    list.add(uname3);
                }
                if(searchVo.getStartamount()!=null){
                    Predicate startamount = cb.greaterThanOrEqualTo(root.get("insurance_amount"), searchVo.getStartamount());
                    list.add(startamount);
                }
                if(searchVo.getEndamount()!=null){
                    Predicate endamount = cb.lessThanOrEqualTo(root.get("insurance_amount"), searchVo.getEndamount());
                    list.add(endamount);
                }
                if(searchVo.getTid()!=null){
                    Predicate tid = cb.equal(root.get("insurance_type_id"), searchVo.getTid());
                    list.add(tid);
                }
                Predicate[] array = list.toArray(new Predicate[list.size()]);
                return cb.and(array);
            }
        };
        PageRequest of = PageRequest.of(pageNum-1, pageSize);
        return insuranceMapper.findAll(spec,of);
    }
    //类型列表
    @Override
    public List<Insurance_type> insuranceTypeList() {
        return insuranceTypeMapper.findAll();
    }
    //添加用户
    @Override
    public <U extends User> U insert(U u) {
        return userMapper.save(u);
    }
    //添加类型
    @Override
    public <T extends Insurance_type> T insert2(T t) {
        return insuranceTypeMapper.save(t);
    }
    //添加保单
    @Override
    public  <S extends Insurance>S insert3(S s) {
        return insuranceMapper.save(s);
    }
}
